package com.zhu.day.interceptor;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.util.List;


public class LogInterceptor implements HandlerInterceptor {

    private static final Logger log = LoggerFactory.getLogger(LogInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        String body = getBody(request);
        if (StringUtils.isBlank(body)){
            body = request.getQueryString();
        }
        log.info("方法名 {}",request.getServletPath()+"  body:"+body);

//        System.out.println(">>>MyInterceptor1>>>>>>>请求处理之后进行调用，但是在视图被渲染之前（Controller方法调用之后）");
        if(response.getStatus()==500){
            System.out.println("/errorpage/500");
        }else if(response.getStatus()==404){
            System.out.println("/errorpage/404");
        }
    }


    public static void main(String[] args) {
        String json = "[\n" +
                "    132535488,\n" +
                "    32486934,\n" +
                "    329813612,\n" +
                "    332906452,\n" +
                "    336400734,\n" +
                "    353819928,\n" +
                "    355865015,\n" +
                "    364646992,\n" +
                "    365739735,\n" +
                "    376244146,\n" +
                "    539778699,\n" +
                "    546385780,\n" +
                "    572223239,\n" +
                "    674271510,\n" +
                "    806493290,\n" +
                "    86977431,\n" +
                "    875249327,\n" +
                "    10029955,\n" +
                "    123745291,\n" +
                "    260083450,\n" +
                "    324345073,\n" +
                "    399820317,\n" +
                "    502024969,\n" +
                "    505300025,\n" +
                "    53615915,\n" +
                "    585134820,\n" +
                "    629262749,\n" +
                "    66274013,\n" +
                "    753179010,\n" +
                "    777445250,\n" +
                "    787680760,\n" +
                "    861808031,\n" +
                "    875516176,\n" +
                "    883901450,\n" +
                "    889603349,\n" +
                "    889781106,\n" +
                "    106757414,\n" +
                "    129224053,\n" +
                "    154291335,\n" +
                "    165078509,\n" +
                "    246376624,\n" +
                "    334077434,\n" +
                "    402725335,\n" +
                "    407777858,\n" +
                "    413505392,\n" +
                "    426670536,\n" +
                "    447504260,\n" +
                "    5058143,\n" +
                "    537564719,\n" +
                "    561044257,\n" +
                "    563623180,\n" +
                "    613664336,\n" +
                "    654595126,\n" +
                "    654632527,\n" +
                "    730253663,\n" +
                "    73147432,\n" +
                "    775347969,\n" +
                "    827739822,\n" +
                "    83478040,\n" +
                "    852066234,\n" +
                "    855839306,\n" +
                "    859793832,\n" +
                "    86236447,\n" +
                "    879690599,\n" +
                "    890318681,\n" +
                "    891134055,\n" +
                "    97100566\n" +
                "]";

        List<String> list = JSON.parseArray(json, String.class);
        System.out.println(JSON.toJSONString(list));



    }

    private String getBody(HttpServletRequest request){
        try {
            BufferedReader br = request.getReader();
            String str;
            StringBuffer body = new StringBuffer("");
            while((str = br.readLine()) != null){
                body.append(str);
            }
            return body.toString();
        }catch (Exception e){
            log.error("获取body出错",e);
            return "{}";
        }
    }

}